﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage<QuanLyNhaThuoc.Models.WarehouseModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <form id="form">
    <div class="box box box-success">
        <div class="box-header">
            <h3 class="box-title">
                Thông tin phiếu nhập</h3>
            <div class="navbar-header">
                <button type="button" class="navbar-toggle hnq navbar-btn sidebar-toggle" data-toggle="collapse"
                    data-target="#import-info-warehouse">
                    <span class="icon-plus"></span><span class="icon-plus"></span><span class="icon-plus">
                    </span>
                </button>
            </div>
        </div>
        <div class="box-body">
            <div class="collapse navbar-collapse remove-collapse" id="import-info-warehouse">
                <div id="modify_ib">
                    <table class="table table-bordered" cellpadding="0" cellspacing="0" border="0">
                        <tbody>
                            <tr>
                                <td>
                                    Số phiếu <span style="color: Red">(*)</span>
                                </td>
                                <td style="width: 35%">
                                    <%: Html.TextBox("IBNumber", Model.IBDetail.IBNumber, new { @class = "form-control restore-width", @disabled = "disabled" })%>
                                </td>
                                <td>
                                    Người nhập
                                </td>
                                <td>
                                    <%: Html.TextBox("UserCreated", Model.IBDetail.UserCreated, new { @class = "form-control restore-width", @disabled = "disabled" })%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Ngày nhập <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <div class="input-group">
                                        <div class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </div>
                                        <%: Html.TextBox("DateCreated", Model.IBDetail.DateCreated, new { @class = "form-control restore-width", @size = 10, @id = "InputDate", @disabled = "disabled" })%>
                                    </div>
                                </td>
                                <td>
                                    Người giao hàng
                                </td>
                                <td>
                                    <%: Html.TextBox("Deliverer", Model.IBDetail.Deliverer, new { @class = "form-control restore-width" })%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Chi nhánh <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <%: Html.DropDownListFor(m => m.SelectedText_Status, Model.ListBranch, new { @class = "select-default", @id = "Branch" })%>
                                </td>
                                <td>
                                    Nhà cung ứng
                                </td>
                                <td>
                                    <%: Html.DropDownListFor(m => m.SelectedValue_Supplier, Model.ListSupplier, new { @class = "select-default", @id = "Supplier" })%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Diễn giải
                                </td>
                                <td colspan="3">
                                    <%: Html.TextArea("Description", Model.IBDetail.Description, new { @class = "form-control", @rows = "3", @cols = "1" })%>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="row" id="radios">
        <div class="col-sm-12">
            <div class="form-group">
                <div class="col-sm-4" id="rdoHeight">
                    <label style="font-weight: normal">
                        <input type="radio" value="First" name="radiocheck" checked="checked" />
                        Nhập lần đầu
                    </label>
                    <label style="font-weight: normal">
                        <input type="radio" value="After" name="radiocheck" />
                        Trong danh sách hàng hóa
                    </label>
                </div>
                <div class="col-md-8" id="searchOn" style="display: none">
                    <div class="input-group" style="margin-bottom: 5px">
                        <div class="input-group-addon">
                            <i class="fa fa-search-minus"></i>
                        </div>
                        <input id="tags" class="form-control" placeholder="Nhập hàng hóa cần tìm..." />
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="box box-success">
        <div class="box-header">
            <h3 class="box-title">
                Chi tiết hàng nhập kho</h3>
            <div class="navbar-header">
                <button type="button" class="navbar-toggle hnq navbar-btn sidebar-toggle" data-toggle="collapse"
                    data-target="#info-warehouse">
                    <span class="icon-plus"></span><span class="icon-plus"></span><span class="icon-plus">
                    </span>
                </button>
            </div>
        </div>
        <div class="box-body" id="hnk">
            <div class="collapse navbar-collapse remove-collapse" id="info-warehouse">
                <div id="hnq">
                    <table class="table table-bordered" cellpadding="0" cellspacing="0" border="0">
                        <tbody>
                            <tr>
                                <td style="width: 15%">
                                    Mã SP <span style="color: Red">(*)</span>
                                </td>
                                <td style="width: 35%">
                                    <%: Html.TextBox("ProductCode", "", new { @class = "form-control restore-width" })%>
                                    <input type="hidden" id="hdfid" />
                                </td>
                                <td style="width: 15%">
                                    Loại thuốc <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <%--<select class="select-default" id="Drug">
                                            <option value="">--Vui lòng chọn--</option>
                                        </select>--%>
                                    <%: Html.DropDownListFor(m => m.SelectedValue_Drug, Model.ListDrug, new { @class = "select-default", @id = "Drug" })%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Tên SP <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <%: Html.TextBox("ProductName", "", new { @class = "form-control" })%>
                                </td>
                                <td>
                                    Số lượng <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <div class="input-group" style="width: 80%">
                                        <%: Html.TextBox("Count","", new { @class = "form-control" })%>
                                        <div class="input-group-btn">
                                            <%--<select class="select-default" id="Unit">
                                                    <option value="">--Đơn vị--</option>
                                                </select>--%>
                                            <%: Html.DropDownListFor(m => m.SelectedValue_Unit, Model.ListUnit, new { @class = "select-default", @id = "Unit" })%>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Giá mua <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <%: Html.TextBox("Buy", "", new { @class = "form-control remove-width" })%>
                                </td>
                                <td>
                                    Đơn vị <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <%: Html.DropDownListFor(m => m.SelectedValue_Currency, Model.ListCurrency, new { @class = "select-default", @id = "Currency" })%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Ngày hết hạn <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <div class="input-group">
                                        <div class="input-group-addon">
                                            <i class="fa fa-calendar"></i>
                                        </div>
                                        <%: Html.TextBox("ExpireDate", "", new { @class = "form-control restore-width", @size = 10, @id = "ExpireDate" })%>
                                    </div>
                                </td>
                                <td>
                                    Trạng thái <span style="color: Red">(*)</span>
                                </td>
                                <td>
                                    <%: Html.DropDownListFor(m => m.SelectedValue_Status, Model.ListStatus, new { @class = "select-default", @id = "Status" })%>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Vị trí đặt
                                </td>
                                <td>
                                    <%: Html.TextBox("Place", "", new { @class = "form-control" })%>
                                </td>
                                <td>
                                    Diễn giải
                                </td>
                                <td>
                                    <%: Html.TextArea("Des_WH", "", new { @class = "form-control", @rows = "2", @cols = "1" })%>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="form-group">
                <div class="btn-toolbar">
                    <button class="btn btn-default" id="Add_Bill">
                        Thêm hàng vào phiếu</button>
                    <button class="btn btn-default" id="Remove-Bill">
                        Hủy hàng khỏi phiếu</button>
                </div>
            </div>
        </div>
    </div>
    <div class="box box-success">
        <div class="box-header">
            <h3 class="box-title">
                Danh sách hàng thuộc phiếu</h3>
        </div>
        <div class="box-body table-responsive no-padding">
            <table id="list" class="table table-hover" cellpadding="0" cellspacing="0" border="0">
                <tbody>
                    <tr class="trfc">
                        <th>
                            <input type="checkbox" id="checkAll" />
                        </th>
                        <th>
                            #
                        </th>
                        <th>
                            Mã SP
                        </th>
                        <th>
                            Tên SP
                        </th>
                        <th>
                            Ngày hết hạn
                        </th>
                        <th>
                            Số lượng
                        </th>
                        <th>
                            Giá mua
                        </th>
                        <th>
                            Đơn vị
                        </th>
                        <th>
                            Trạng thái
                        </th>
                    </tr>
                    <% if (Session["data"] != null)
                       {%>
                    <% foreach (var item in (IEnumerable<QuanLyNhaThuoc.Models.GetListWarehouseByIBID_Result>)Session["data"])
                       { %>
                    <tr>
                        <td align="center">
                            <input type="checkbox" />
                        </td>
                        <td align="center">
                            <%: item.ID + "." %>
                        </td>
                        <td>
                            <%: item.ProductCode%>
                        </td>
                        <td>
                            <%: item.ProductName %>
                        </td>
                        <td align="center">
                            <%: item.ExpireDate %>
                        </td>
                        <td align="center">
                            <%= QuanLyNhaThuoc.HelperClass.Common.FormatMoneyDisplay((item.COUNT == null ? "0" : item.COUNT.ToString()), item.CCYID) %>
                        </td>
                        <td align="center">
                            <%= QuanLyNhaThuoc.HelperClass.Common.FormatMoneyDisplay((item.Buy == null ? "0" : item.Buy.ToString()), item.CCYID)%>
                        </td>
                        <td align="center">
                            <%: item.STATUSCCYID%>
                        </td>
                        <td align="center">
                            <span class="label label-success">
                                <%: item.SelectedText_Status%></span>
                        </td>
                    </tr>
                    <% } %>
                    <% } %>
                </tbody>
            </table>
        </div>
    </div>
    </form>
    <div class="row" style="text-align: center">
        <div class="col-sm-12">
            <div class="form-group">
                <div class="btn-toolbar">
                    <button class="btn btn-default" id="Save-Bill">
                        Lưu phiếu</button>
                    <button class="btn btn-default" onclick="window.location.href = '/Transaction/Warehouse'">
                        Thoát</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Library -->
    <script src="/Scripts/jquery-1.5.1.js" type="text/javascript"></script>
    <!-- jQuery 2.0.2 -->
    <script src="/Scripts/Pharma/jquery.min.js" type="text/javascript"></script>
    <!-- jQuery UI 1.10.3 -->
    <script src="/Scripts/Pharma/jquery-ui-1.10.3.min.js" type="text/javascript"></script>
    <!-- Bootstrap -->
    <script src="/Scripts/Pharma/bootstrap.min.js" type="text/javascript"></script>
    <!-- Bootstrap WYSIHTML5 -->
    <script src="/Scripts/Pharma/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"
        type="text/javascript"></script>
    <!-- AdminLTE App -->
    <script src="/Scripts/Pharma/AdminLTE/app_permission.js" type="text/javascript"></script>
    <!-- InputMask -->
    <script src="/Scripts/Pharma/plugins/input-mask/jquery.inputmask.js" type="text/javascript"></script>
    <script src="/Scripts/Pharma/plugins/input-mask/jquery.inputmask.date.extensions.js"
        type="text/javascript"></script>
    <script src="/Scripts/Pharma/plugins/input-mask/jquery.inputmask.extensions.js" type="text/javascript"></script>
    <!-- Notification -->
    <script src="/Scripts/Pharma/plugins/notification/jquery.noty.packaged.min.js" type="text/javascript"></script>
    <script src="/Scripts/Pharma/plugins/notification/Notification.js" type="text/javascript"></script>
    <!-- Combogrid -->
    <script src="/Scripts/Pharma/plugins/Combogrid/jquery-ui-1.10.1.custom.min.js" type="text/javascript"></script>
    <link href="/Content/themes/Pharma/smoothness/jquery-ui-1.10.1.custom.css" rel="stylesheet"
        type="text/css" />
    <script src="/Scripts/Pharma/plugins/Combogrid/jquery.ui.combogrid-1.6.3.js" type="text/javascript"></script>
    <link href="/Content/themes/Pharma/smoothness/jquery.ui.combogrid.css" rel="stylesheet"
        type="text/css" />
    <!-- Validate Engine -->
    <link href="/Content/themes/ValidationEngine/validationEngine.jquery.css" rel="stylesheet"
        type="text/css" />
    <script src="/Scripts/Pharma/plugins/ValidationEngine/jquery.validationEngine-vi.js"
        type="text/javascript"></script>
    <script src="/Scripts/Pharma/plugins/ValidationEngine/jquery.validationEngine.js"
        type="text/javascript"></script>
    <!-- format Currency -->
    <script src="/Scripts/Pharma/plugins/formatCurrency/jquery.price_format.1.8.min.js"
        type="text/javascript"></script>
    <script src="/Scripts/Pharma/Pharmacy.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            Pharma._init();
            Pharma._autocomplete("tags");
            Pharma._scroll("list", 250);
            Pharma._addActive(["tvTransaction", "liWarehouse"], "ulTransaction");
            Pharma._checkAll("checkAll");
            Pharma._bind(["IBNumber", "Branch", "ProductCode", "ProductName", "Buy", "Sell", "Drug", "Count", "Unit", "Currency", "Status"], "validate");
            Pharma._bind(["InputDate"], "maskwithoutvalidate");
            Pharma._bind(["ExpireDate"], "mask");
            Pharma._bind(["Buy", "Sell", "Count"], "moneyFormat");
            Pharma._animate($("input:radio[name=radiocheck]"));
            var flag = 0;
            var lString = [];           

            $("#ProductCode").change(function (e) {
                e.preventDefault();
                if ($(this).val() != "") {
                    $.ajax({
                        type: 'POST',
                        url: '/Transaction/Check_ProductCode',
                        data: { pCode: $(this).val() },
                        dataType: 'JSON',
                        success: function (rs) {
                            if (rs.status[0].RESULT == 1) {
                                jQuery("#ProductCode").validationEngine('showPrompt', '* Mã sản phẩm đã tồn tại', 'error').focus();
                                flag = 0;
                            }
                            else {
                                flag = 1;
                            }
                        },
                        error: function (rs) {
                            var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra !', type: 'error', timeout: 2000 });
                        },
                        traditional: true
                    });
                }
                else {
                    if (jQuery('#form').validationEngine('validate')) {
                        return false;
                    }
                }
            });

            $("#Add_Bill").click(function (e) {
                e.preventDefault();
                try {
                    if (Pharma._bound(["ExpireDate", "InputDate"], 'Add_Bill') != false) {
                        if (flag == 1) {
                            var js_wh_model = { IBNumber: $("#IBNumber").val(), UserCreated: $("#UserCreated").val(), InputDate: $("#LoadAllCodeByName").val(), Deliverer: $("#Deliverer").val(), SelectedValue_Store: $("#Store option:selected").val(), SelectedValue_Supplier: $("#Supplier option:selected").val(), Des_IB: $("#Description").val(), ProductID: $("#hdfid").val(), ProductCode: $("#ProductCode").val(), SelectedValue_Drug: $("#Drug option:selected").val(), ProductName: $("#ProductName").val(), Count: $("#Count").val(), SelectedValue_Unit: $("#Unit option:selected").val(), Buy: $("#Buy").val(), ExpireDate: $("#ExpireDate").val(), SelectedValue_Status: $("#Status option:selected").val(), SelectedText_Status: $("#Status option:selected").text(), CCYID: $("#Currency option:selected").val(), STATUSCCYID: $("#Currency option:selected").text(), Place: $("#Place").val(), Des_WH: $("#Des_WH").val() };
                            $.ajax({
                                type: 'POST',
                                url: '/Transaction/Warehouse_Creating',
                                data: { jsonData: JSON.stringify(js_wh_model), productCode: js_wh_model.ProductCode },
                                dataType: 'JSON',
                                success: function (rs) {
                                    switch (rs.status) {
                                        case 0:
                                            $("#tags").val("");
                                            $("#hnk input").val("");
                                            $("#Deliverer").val("");
                                            $("#hnk select option").filter("[value='']").prop("selected", true);
                                            $("#modify_ib select option").filter("[value='']").prop("selected", true);

                                            $("#list tbody").find("tr:gt(0)").remove();
                                            for (var i = 0; i < rs.data.length; i++) {
                                                $("#list tbody").append('<tr><td align="center"><input id type="checkbox" /></td><td align="center">' + rs.data[i].ID + '.</td><td style="white-space: normal">' + rs.data[i].ProductCode + '</td><td style="white-space: normal">' + rs.data[i].ProductName + '</td><td align="center">' + rs.data[i].ExpireDate + '</td><td align="center">' + Pharma._formatCurrency(rs.data[i].COUNT) + '</td><td align="center">' + Pharma._formatCurrency(rs.data[i].Buy) + '</td><td align="center">' + rs.data[i].STATUSCCYID + '</td><td align="center"><span class="label label-success">' + rs.data[i].SelectedText_Status + '</span></td></tr>').slideDown('slow');
                                            }
                                            Pharma._scroll("list", 250);
                                            $('html').animate({ scrollTop: $(document).height() }, 1000);
                                            var n = noty({ layout: 'topRight', text: 'Bạn đã thêm mới sản phẩm ' + js_wh_model.ProductName + ' thành công', type: 'success', timeout: 2000 });
                                            break;
                                        case 1:
                                            var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                                            break;
                                        case 2:
                                            jQuery("#ProductCode").validationEngine('showPrompt', '* Trùng mã sản phẩm', 'error').focus();
                                            break;
                                    }
                                },
                                error: function (rs) {
                                    var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                                },
                                traditional: true
                            });
                        }
                        else {
                            jQuery("#ProductCode").validationEngine('showPrompt', '* Mã sản phẩm đã tồn tại', 'error').focus();
                        }
                    }
                }
                catch (err) {
                    var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                }
            });

            $("#Remove-Bill").click(function (e) {
                e.preventDefault();
                try {
                    if ($("#list > tbody > tr").length > 1) {
                        if ($("#list input:checkbox:not(#checkAll):checked").length > 0) {
                            $("#list input:checkbox:not(#checkAll):checked").each(function (i, v) {
                                lString.push($(v).parent('td').next('td').next('td').text());
                            });

                            $.ajax({
                                type: 'POST',
                                url: '/Transaction/Warehouse_Removing',
                                data: { jsonData: JSON.stringify(lString) },
                                dataType: 'JSON',
                                success: function (rs) {
                                    if (rs.status == 0) {
                                        $("#list tbody").find("tr:gt(0)").remove();
                                        for (var i = 0; i < rs.data.length; i++) {
                                            $("#list tbody").append('<tr><td align="center"><input id type="checkbox" /></td><td align="center">' + rs.data[i].ID + '.</td><td style="white-space: normal">' + rs.data[i].ProductCode + '</td><td style="white-space: normal">' + rs.data[i].ProductName + '</td><td align="center">' + rs.data[i].ExpireDate + '</td><td align="center">' + Pharma._formatCurrency(rs.data[i].COUNT) + '</td><td align="center">' + Pharma._formatCurrency(rs.data[i].Buy) + '</td><td align="center">' + rs.data[i].STATUSCCYID + '</td><td align="center"><span class="label label-success">' + rs.data[i].SelectedText_Status + '</span></td></tr>').slideDown('slow');
                                        }
                                        Pharma._scroll("list", 250);
                                        $("#checkAll").prop('checked', false);
                                        $('html').animate({ scrollTop: $(document).height() }, 1000);
                                        var n = noty({ layout: 'topRight', text: 'Bạn đã hủy hàng khỏi phiếu thành công', type: 'success', timeout: 2000 });
                                    } else {
                                        var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                                    }
                                },
                                error: function (rs) {
                                    var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                                },
                                traditional: true
                            });
                        }
                        else {
                            var n = noty({ layout: 'topRight', text: '<strong>Lưu ý</strong><br/>Bạn chưa chọn đơn hàng', type: 'warning', timeout: 2000 });
                        }
                    } else {
                        var n = noty({ layout: 'topRight', text: '<strong>Lưu ý</strong><br/>Danh sách hàng thuộc phiếu trống !', type: 'warning', timeout: 2000 });
                    }
                }
                catch (err) {
                    var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                }
            });

            $("#Save-Bill").click(function (e) {
                e.preventDefault();
                try {
                    $.post('/Transaction/Warehouse_Saving', function (rs) {
                        if (rs.status == 0) {                           
                            $("#checkAll").prop('checked', false);
                            $("#list tbody").find("tr:gt(0)").remove();
                            for (var i = 0; i < rs.data.length; i++) {
                                $("#list tbody").append('<tr><td align="center"><input id type="checkbox" /></td><td align="center">' + rs.data[i].ID + '.</td><td style="white-space: normal">' + rs.data[i].ProductCode + '</td><td style="white-space: normal">' + rs.data[i].ProductName + '</td><td align="center">' + rs.data[i].ExpireDate + '</td><td align="center">' + Pharma._formatCurrency(rs.data[i].COUNT) + '</td><td align="center">' + Pharma._formatCurrency(rs.data[i].Buy) + '</td><td align="center">' + rs.data[i].STATUSCCYID + '</td><td align="center"><span class="label label-success">' + rs.data[i].SelectedText_Status + '</span></td></tr>').slideDown('slow');
                            }
                            Pharma._scroll("list", 250);
                            $('html').animate({ scrollTop: $(document).height() }, 1000);
                            var n = noty({ layout: 'topRight', text: 'Bạn đã lưu phiếu thành công', type: 'success', timeout: 2000 });
                        } else {
                            var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                        }
                    });
                } catch (err) {
                    alert(err);
                    var n = noty({ layout: 'topRight', text: 'Có lỗi xảy ra', type: 'error', timeout: 2000 });
                }
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="NavBar" runat="server">
    <h1>
        Sửa phiếu <small>Control panel</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>Home</a></li>
        <li class="active">Giao dịch</li>
        <li class="active">Sửa phiếu</li>
    </ol>
</asp:Content>
